#include "WinThread.h"


WinThread::WinThread(void)
{
}

WinThread::~WinThread(void)
{
	DWORD lpdwFlags[100];
	if (GetHandleInformation(m_handle, lpdwFlags)!= 0)
	{
		LogController::getInstance()->AddToLog(LogController::L_WARNING, "Force for close thread");
		CloseHandle(m_handle);
	}
}

void WinThread::create(void *function, void *data)
{
	if ((m_handle = CreateThread( 
		NULL,
		0,
		*(LPTHREAD_START_ROUTINE )function,
		data,
		0,
		&m_dwThreadId)) == NULL)
		throw ServerException("Can't create thread");

}

void WinThread::join()
{
	LogController::getInstance()->AddToLog(LogController::L_NOTICE, "Join thread");
	WaitForSingleObject(m_handle, INFINITE);
}

void WinThread::cancel()
{
	LogController::getInstance()->AddToLog(LogController::L_NOTICE, "Close thread");
	CloseHandle(m_handle);
}